package com.chushouya.controller.manager.admin;

import com.chushouya.common.constant.ServiceType;
import com.chushouya.manager.dto.admin.config.ConfigDTO;
import com.chushouya.manager.dto.admin.config.ConfigListDTO;
import com.chushouya.manager.dto.admin.config.ConfigQuery;
import com.chushouya.manager.dto.admin.config.ConfigSaveDTO;
import com.chushouya.manager.service.admin.ConfigService;
import com.general.framework.core.enums.BusinessType;
import com.general.framework.core.schema.PageResult;
import com.general.framework.core.schema.Response;
import com.general.framework.web.annotations.Log;
import com.general.framework.web.annotations.RepeatSubmit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Api(tags = ServiceType.PLATFORM + "参数配置")
@RestController
@RequestMapping(ServiceType.PLATFORM + "/config")
public class ConfigController {
    @Resource
    private ConfigService configService;

    @ApiOperation("获取参数配置列表")
    @GetMapping("/list")
    public Response<PageResult<ConfigListDTO>> list(ConfigQuery query) {
        return Response.success(configService.selectConfigPageResult(query));
    }

    @ApiOperation("根据参数编号获取详细信息")
    @GetMapping("/getByConfigId")
    public Response<ConfigSaveDTO> getByConfigId(Long configId) {
        return Response.success(configService.selectConfigById(configId));
    }

    @ApiOperation("根据参数键名查询参数值")
    @GetMapping("/getByConfigKey")
    public Response<ConfigDTO> getConfigKey(String configKey) {
        return Response.success(configService.selectConfigByKey(configKey));
    }

    @ApiOperation("新增参数配置")
    @Log(businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @RepeatSubmit
    public Response<Long> add(@Validated @RequestBody ConfigSaveDTO config) {
        return Response.success(configService.addConfig(config));
    }

    @ApiOperation("修改参数配置")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Response<Long> edit(@Validated @RequestBody ConfigSaveDTO config) {
        return Response.success(configService.editConfig(config));
    }

    @ApiOperation("删除参数配置")
    @Log(businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    public Response<Void> remove(Long configId) {
        configService.removeConfig(configId);
        return Response.success();
    }

    @ApiOperation("刷新参数缓存")
    @Log(businessType = BusinessType.UPDATE)
    @PostMapping("/refreshCache")
    public Response<Void> refreshCache() {
        configService.resetConfigCache();
        return Response.success();
    }
}
